package com.adxon.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ServletBase extends HttpServlet{
	private static Map<String,Boolean>	aps;
	public static String				authentication="application";
	public static String				copyright;
	public static boolean				debug;
	public static String				footer="<ul><li>Footer</ul>";
	public static String				header="<a href=/>Header</a>";
	private static boolean				ini=true;
	public static String				jsp="/jsp/";
	public static int					site=999;
	public static String				title="Title";

	public static void cacheControl(HttpServletResponse resp, long seconds){
		resp.setHeader("Cache-Control","max-age="+seconds);
		resp.setDateHeader("Expires",System.currentTimeMillis()+seconds*1000);
	}
	public static void delay(int time){
		try{
			Thread.sleep(time);
		}catch(InterruptedException e){}
	}
	public static void forward(HttpServletRequest req,
			HttpServletResponse resp)throws ServletException,IOException{
		forward((String)req.getAttribute("Page"),jsp,req,resp);
	}
	public static void forward(String page,HttpServletRequest req,
			HttpServletResponse resp)throws ServletException,IOException{
		forward(page,jsp,req,resp);
	}
	public static void forward(String page,String path,HttpServletRequest req,
			HttpServletResponse resp)throws ServletException,IOException{
		System.out.println("Forward: "+path+page);
		req.getRequestDispatcher(path+page).forward(req,resp);
	}
	public static void forward403(HttpServletRequest req,
			HttpServletResponse resp)throws ServletException,IOException{
		try{
			Thread.sleep(1000);
			setAttributes(req,"Not fount");
			forward("403.jsp",req,resp);
		}catch(InterruptedException e){}
	}
	public static void forward404(HttpServletRequest req,
			HttpServletResponse resp)throws ServletException,IOException{
		try{
			Thread.sleep(1000);
			setAttributes(req,"Not fount");
			forward("404.jsp",req,resp);
		}catch(InterruptedException e){}
	}
	public static String getString(InputStream stream)throws IOException{
		byte[] b=new byte[stream.available()];
		stream.read(b);
		return new String(b,"UTF-8");
	}
	public static void init(ServletContext context){
		if(ini){
			ini=false;
			aps=new ConcurrentHashMap<String,Boolean>();
			String s=context.getInitParameter("authentication");
			if(s!=null)
				authentication=s;
			s=context.getInitParameter("copyright");
			if(s!=null)
				copyright=s;
			s=context.getInitParameter("debug");
			if(s!=null)
				footer=s;
			if(s!=null){
				char c=s.charAt(0);
				if(c=='Y' || c=='y')
					debug=true;
			}
			s=context.getInitParameter("footer");
			if(s!=null)
				footer=s;
			s=context.getInitParameter("header");
			if(s!=null)
				header=s;
			s=context.getInitParameter("jsp-path");
			if(s!=null)
				jsp=s;
			s=context.getInitParameter("site-id");
			if(s!=null)
				site=Integer.parseInt(s);
			s=context.getInitParameter("title");
			if(s!=null)
				title=s;
		}
	}
	protected static void print(HttpServletResponse resp,String string)
			throws IOException{
		resp.getWriter().print(string);
	}
	protected static void print(HttpServletResponse resp,StringBuilder builder)
			throws IOException{
		resp.getWriter().print(builder);
	}
	public static void registerApp(String appName){
		aps.put(appName,true);
	}
	public static void setAttributes(HttpServletRequest req,String title){
		if(title==null)
			title=ServletBase.title;
		req.setAttribute("Copyright",copyright);
		req.setAttribute("Footer",footer);
		req.setAttribute("Head",title);
		req.setAttribute("Header",header);
		req.setAttribute("Title",title);
	}

//	private boolean app;

	public void init()throws ServletException{
		super.init();
		init(getServletContext());
		System.out.println("ServletBase: init - "+getServletContext().getContextPath());
	}
/*	protected void registerApp(HttpServletRequest req)throws UnsupportedEncodingException{
		if(!app){
			app=true;
			Uri i=new Uri(req);
			Adxon.registerApp(i.getPath());
		}
	}*/
}